*ssc install  mmat2tex
set more 1
use ..\data\paper_data.dta, clear
drop if treatment_id==3

preserve

* Table 2: Descriptive statistics and tests of economy-level variables
*=====================================================================

collapse (mean) g gini earnings_tot, by (treatment_id part economy_id economy_type)
mat A=J(1,9,.)
ds g earnings_tot gini
foreach v in `r(varlist)'{
		mat B=J(1,9,.)
		sum `v' if treatment_id==0 & part==1
		mat B[1,1]=r(mean)
		mat B[1,2]=r(sd)
		mat B[1,3]=.
		sum `v' if treatment_id==1 & part==1
		mat B[1,4]=r(mean)
		mat B[1,5]=r(sd)
		ranksum `v' if treatment_id!=2 & part==1, by(treatment_id)
		mat B[1,6]=r(p)
		sum `v' if treatment_id==2 & part==1
		mat B[1,7]=r(mean)
		mat B[1,8]=r(sd)
		ranksum `v' if treatment_id!=1 & part==1, by(treatment_id)
		mat B[1,9]=r(p)
		mat A=[A \ B]	
}
		mat B=J(1,9,.)
		sum g if treatment_id==0 & part==1
		mat B[1,1]=r(N)
		sum g if treatment_id==1 & part==1
		mat B[1,4]=r(N)
		sum g if treatment_id==2 & part==1
		mat B[1,7]=r(N)
		mat A=[A \ B]	
ds g earnings_tot gini
foreach v in `r(varlist)'{
		mat C=J(1,9,.)
		sum `v' if treatment_id==0 & part==2
		mat C[1,1]=r(mean)
		mat C[1,2]=r(sd)
		mat C[1,3]=.
		sum `v' if treatment_id==1 & part==2
		mat C[1,4]=r(mean)
		mat C[1,5]=r(sd)
		ranksum `v' if treatment_id!=2 & part==2, by(treatment_id)
		mat C[1,6]=r(p)
		sum `v' if treatment_id==2 & part==2
		mat C[1,7]=r(mean)
		mat C[1,8]=r(sd)
		ranksum `v' if treatment_id!=1 & part==2, by(treatment_id)
		mat C[1,9]=r(p)
		mat A=[A \ C]	
}
		mat C=J(1,9,.)
		sum g if treatment_id==0 & part==2
		mat C[1,1]=r(N)
		sum g if treatment_id==1 & part==2
		mat C[1,4]=r(N)
		sum g if treatment_id==2 & part==2
		mat C[1,7]=r(N)
		mat A=[A \ C]	
ds g earnings_tot gini
foreach v in `r(varlist)'{
		mat D=J(1,9,.)
		sum `v' if treatment_id==0 & part==3
		mat D[1,1]=r(mean)
		mat D[1,2]=r(sd)
		mat D[1,3]=.
		sum `v' if treatment_id==1 & part==3
		mat D[1,4]=r(mean)
		mat D[1,5]=r(sd)
		ranksum `v' if treatment_id!=2 & part==3, by(treatment_id)
		mat D[1,6]=r(p)
		sum `v' if treatment_id==2 & part==3
		mat D[1,7]=r(mean)
		mat D[1,8]=r(sd)
		ranksum `v' if treatment_id!=1 & part==3, by(treatment_id)
		mat D[1,9]=r(p)
		mat A=[A \ D]	
}
		mat D=J(1,9,.)
		sum g if treatment_id==0 & part==3
		mat D[1,1]=r(N)
		sum g if treatment_id==1 & part==3
		mat D[1,4]=r(N)
		sum g if treatment_id==2 & part==3
		mat D[1,7]=r(N)
		mat A=[A \ D]	
mat list A
local colnames `"colnames("Mean" "S.D." " p " "Mean" "S.D." " p " "Mean" "S.D." " p ")"'
local rownames "rownames(a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 b0 b1 b2)"
local substitutes `"substitute(a0 " \emph{Part 1} " a1 "Global geoengineering" a2 "Total surplus" a3 "Gini index" a4 "Observations" a5 "Global geoengineering" a6 "Total surplus" a7 "Gini index" a8 "Observations" a9 "Global geoengineering" b0 "Total surplus" b1 "Gini index" b2 "Observations")"'
local beginning "\begin{table}[h!]\centering"
local end1 "\bottomrule\end{tabular}\begin{tablenotes}[para]\footnotesize"
local end2 "\end{tablenotes}\end{threeparttable}\end{table}"
mata: A=st_matrix("A")
local pre_header `"preheader(`beginning'\caption{\textbf{Descriptive statistics and tests \label{tab:test}}}\begin{threeparttable}\begin{tabular}{lr@{ }llr@{ }llr@{ }ll}\toprule &\multicolumn{2}{c}{Baseline}&&\multicolumn{2}{c}{Transfer}&&\multicolumn{2}{c}{Structured}&\\)"'
local bottom `"bottom( `end1' \item \textit{Note:} $ p $-values refer to Wilcoxon-Mann-Whitney exact tests of Baseline vs. Decentralized and Baseline vs. Structured; the null hypothesis is that the samples come from the same population. The unit of observation corresponds to an economy of six in Part 3; the unit of observation is the average of the three types of economy of two in Parts 1 and 2. Global geoengineering and Total surplus are computed as the average outcome in an economy in a given part of the experiment; the Gini index is computed on cumulative earnings in the last period of each part. `end2' )"'
mmat2tex A using ..\tabs\decriptives.tex, replace fmt(%12.3f) insertendrow(2 " \midrule " 7 "\addlinespace  \emph{Part 2} & \\ " 11 "\addlinespace  \emph{Part 3} & \\ ")  `colnames' `rownames' `substitutes' `pre_header' `bottom'

restore
preserve

* Table 3: Frequency of offers and side-payments (the table was manually constructed in tex)
*===============================================

* N=2
collapse transfers_sent request_team high_gstar if part==2 & treatment_id>0 & treatment_id<3, by(treatment_id economy_id team_id gstar period)

gen transfer_enacted=transfers_sent>0 & transfers_sent<.
gen offer_made=request_team>=0 & request_team<.
gen hightolow_transfer=transfers_sent>0 & transfers_sent<. & high_gstar==1
collapse (sum)hightolow_* transfer_enacted offer_made, by(treatment_id economy_id period)
gen category=.
replace category=0 if offer_made==0
replace category=1 if offer_made>0 & transfer_enacted==0
replace category=2 if offer_made>0 & transfer_enacted==1 & hightolow_transfer==0
replace category=3 if offer_made>0 & transfer_enacted==1 & hightolow_transfer==1
label define category /// 
  0 "No offer" /// 
  1 "Offer not taken" ///
  2 "From team w/lowest ideal point" ///
  3 "From team w/highest ideal point", replace 
label value category category
tab category treatment_id, mis col // output N=2

* N=6
restore
preserve
collapse transfers_sent request_team if part==3 & treatment_id>0 & treatment_id<3, by(treatment_id economy_id team_id gstar period)
gen transfer_enacted=transfers_sent>0 & transfers_sent<.
gen offer_made=request_team>=0 & request_team<.
gen hightolow_transfer=transfers_sent>0 & gstar==10
gen hightolow_offer=request_team>=0 & request_team<. & gstar==10
collapse (sum)hightolow_* transfer_enacted offer_made, by(treatment_id economy_id period)
gen category=.
replace category=0 if offer_made==0
replace category=1 if offer_made>0 & transfer_enacted==0
replace category=2 if offer_made>0 & transfer_enacted==1
replace category=3 if offer_made>0 & transfer_enacted>1
replace category=4 if offer_made>0 & transfer_enacted>2
label define category /// 
  0 "No offer" /// 
  1 "Offer not taken" ///
  2 "From one team" ///
  3 "From two teams" /// 
  4 "From more than two teams", replace 
label value category category
tab category treatment_id, mis col // output N=6